var fishfactory = require('../module/fishfactory')();
var GameData = require('../module/gameData')();
cc.Class({
    extends: cc.Component,

    properties: {
        richtext: cc.Label,


    },
    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        this.bool = false;
        this.gameSence = null;
        var _Allfish = this.node.parent.parent.parent;
        if (_Allfish.parent.name == "coin_layer") {
            this.moveHome();
            this.gameSence = "Home";
        } else {
            this.gameSence = "play";
            this.move();
        }
    },
    move() {
        var _Allfish = this.node.parent.parent.parent;
        this.shuimuArr = [];
        _Allfish.move = true;
        this.playtype = "move";
        var that = this;
        var Arr = [];

        console.log(_Allfish.parent.name);
        var fishArr = GameData.GameLayer.fishArr;
        for (var a = 0; a < fishArr.length; a++) {
            var obj = fishArr[a];
            if (obj.id == 12 || obj.id == 13 || obj.id == 14) {
                if (obj.movetype) {
                    Arr.push(obj.movetype)
                }
            }
        }
        var Arr1 = [1, 2, 3, 4, 5, 6, 7];
        for (var a = 0; a < Arr1.length; a++) {
            if (Arr1.indexOf(Arr[a]) != -1) {
                Arr1.splice(Arr1.indexOf(Arr[a]), 1);
                a--;
            }
        }
        var idx = Math.floor(Math.random() * Arr1.length);
        var type = Arr1[idx];
        // var type = 7;
        var _Allfish = this.node.parent.parent.parent;
        _Allfish.movetype = type;
        var action;
        switch (type) {
            case 1:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(2, 0, 300).easing(cc.easeSineOut()),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(1, 0, -150).easing(cc.easeInOut(1)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(3, 0, 350).easing(cc.easeInOut(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(3, 0, 300).easing(cc.easeInOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(2, 0, 200).easing(cc.easeInOut(2)),
                )
                break;
            case 2:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(4, 0, 400).easing(cc.easeSineOut()),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -150).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(6, 0, 600).easing(cc.easeSineOut(6)),
                )
                break;
            case 3:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(3, 0, 300).easing(cc.easeSineOut()),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -200).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(3, 0, 300).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -150).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0)
                        }
                    ),
                    cc.moveBy(2, 0, 500).easing(cc.easeSineOut(2)),
                )
                break;
            case 4:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(-200, 300);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, -200, 300).easing(cc.easeSineOut()),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0);
                            var _jiaodu = that.getAngle(0, -200);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(2, 0, -200).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(300, 300);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, 300, 300).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0);
                            var _jiaodu = that.getAngle(0, -150);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(2, 0, -150).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(200, 500);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(2, 200, 500).easing(cc.easeSineOut(2)),
                )
                break;
            case 5:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                        }
                    ),
                    cc.moveBy(3, 0, 300).easing(cc.easeSineOut()),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0);
                        }
                    ),
                    cc.moveBy(2, 0, -200).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(-200, 200);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(2, -200, 200).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(300, 300);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, 300, 300).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -100).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            that.node.rotation = 0;
                        }
                    ),
                    cc.moveBy(1, 0, 400).easing(cc.easeSineOut(1)),
                )
                break;
            case 6:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(200, 300);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, 200, 300).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(0, 200);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(2, 0, 200).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(-300, 300);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, -300, 300).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -150).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            that.node.rotation = 0;
                        }
                    ),
                    cc.moveBy(2, 0, 400).easing(cc.easeSineOut(200)),
                )
                break;
            case 7:
                action = cc.sequence(
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(100, 200);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, 100, 200).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -150).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(-300, 200);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(2, -300, 200).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(0, 350);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, 0, 350).easing(cc.easeSineOut(3)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0)
                        }
                    ),
                    cc.moveBy(2, 0, -150).easing(cc.easeIn(2)),
                    cc.callFunc(
                        function () {
                            that.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("move", 0);
                            var _jiaodu = that.getAngle(100, 300);
                            that.node.rotation = _jiaodu;
                        }
                    ),
                    cc.moveBy(3, 100, 300).easing(cc.easeSineOut(3)),
                )
                break;
        }
        ;
        _Allfish.runAction(
            cc.sequence(
                action,
                cc.callFunc(function () {
                    var _Allfish = that.node.parent.parent.parent;
                    fishfactory.poolFish(_Allfish);
                })
            ),
        );
    },
    getAngle(x, y) {
        var _hudu = Math.atan(Math.abs(y / x));
        var _jiaodu = _hudu * 180 / 3.14;
        if (x < 0) {
            _jiaodu = -90 + _jiaodu;
        } else {
            _jiaodu = 90 - _jiaodu;
        }
        return _jiaodu;
    },
    updateMove() {
        if (!GameData.isPause) {
            if (!this.bool) {
                this.bool = true;
                var _Allfish = this.node.parent.parent.parent;
                console.log(this.playtype);
                this.node.getComponent(dragonBones.ArmatureDisplay).playAnimation(this.playtype, 0);
                _Allfish.resumeAllActions();
            }
        } else {
            if (this.bool) {
                this.bool = false;
                this.playtype = this.node.getComponent(dragonBones.ArmatureDisplay).animationName;
                this.node.getComponent(dragonBones.ArmatureDisplay).playAnimation("idle", 0);
                var _Allfish = this.node.parent.parent.parent;
                _Allfish.pauseAllActions();
            }
        }
    },
    moveHome() {
        var _Allfish = this.node.parent.parent.parent;
        _Allfish.x = Math.floor(Math.random() * 1136 - 568);
        _Allfish.y = Math.floor(Math.random() * 80 + 320);
        _Allfish.hometime = 3;
    },
    updateHome(dt) {
        var sp = this.node.parent.parent.parent;
        if (sp.hometime >= 1) {
            sp.hometime = 0;
            sp._y = Math.floor(Math.random() * 1 - 2);
            sp._x = Math.floor(Math.random() * 1 - 2);
        } else {
            sp.hometime += dt;
        }
        if (sp.y >=-200&&sp.y <= 200) {
            sp.y += sp._y;
        } else if(sp.y > 300) {
            sp.y=300;
            sp._y=-sp._y
            sp.y += sp._y;
        }else {
            sp.y=-300;
            sp._y=-sp._y
            sp.y += sp._y;
        }
        if (sp.x >=-700&&sp.y <= 700) {
            sp.x += sp._x;
        } else if(sp.y > 700) {
            sp.x=700;
            sp._x=-sp._x
            sp.x += -sp._x;
        }else {
            sp.x=-700;
            sp._x=-sp._x
            sp.x += -sp._x;

        }


    },
    start() {
    },

    restObj(obj) {
        obj.idx = 0;
        obj.move = true;
        obj._y = 1;
        obj._x = 0;
        obj.rotation = 0;
        obj.down = false;
        obj.speed = false;
    },
    restIdx(obj) {
        obj.idx++;
        if (obj.idx >= obj.h_Arr.length) {
            obj.move = false;
        }
    },
    update(dt) {

        if (this.gameSence == "play") {
            this.updateMove();

        } else {
            this.updateHome(dt);
        }
    }
});
